package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminAdminListVo;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author stone
 * @date 2022/09/06 11:44
 */
@Service
public class AdminServiceImpl implements AdminService{

    @Autowired
    MarketAdminMapper adminMapper;

    @Override
    public MarketAdmin queryById(Integer id) {
        MarketAdmin marketAdmin = adminMapper.selectByPrimaryKey(id);
        return marketAdmin;
    }

    @Override
    public void modify(MarketAdmin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public CommonData<AdminAdminListVo> list(BasePageInfo pageInfo, String username) {
        PageHelper.startPage(pageInfo.getPage(),pageInfo.getLimit());
        val adminExample = new MarketAdminExample();
        val criteria = adminExample.createCriteria();
        if (username != null && !username.trim().equals("")){
            // 根据名字做模糊查询
            criteria.andUsernameLike("%"+username+"%");
        }
        // 查询非删除账号
        criteria.andDeletedEqualTo(false);
        // 排序
        adminExample.setOrderByClause(pageInfo.getSort() + " " + pageInfo.getOrder());
        val admins = adminMapper.selectByExample(adminExample);
        val adminPageInfo = new PageInfo<>(admins);

        // 构造返回数据
        val adminListVoCommonData = new CommonData<AdminAdminListVo>();
        adminListVoCommonData.setPage(pageInfo.getPage());
        adminListVoCommonData.setTotal((int) adminPageInfo.getTotal());
        adminListVoCommonData.setPages(adminPageInfo.getPageSize());
        adminListVoCommonData.setLimit(pageInfo.getLimit());
        val adminAdminListVos = admins.stream()
                .map(o -> new AdminAdminListVo(o.getId(), o.getUsername(), o.getAvatar(),
                        o.getRoleIds())).collect(Collectors.toList());
        adminListVoCommonData.setList(adminAdminListVos);
        return adminListVoCommonData;
    }

    @Override
    public MarketAdmin addAdmin(MarketAdmin marketAdmin) {
        Date date = new Date();
        marketAdmin.setAddTime(date);
        marketAdmin.setUpdateTime(date);
        // 插入数据
        adminMapper.insertSelective(marketAdmin);

        // 根据自增主键id查询数据
        MarketAdmin admin = adminMapper.selectByPrimaryKey(marketAdmin.getId());
        return admin;
    }

    /**
     * @description: 查询所有非删除的admin
     * @return: java.util.List<com.cskaoyan.bean.MarketAdmin>
     * @author: JianhuaZeng
     * @date: 2022/12/30 2022/12/30
     */
    public List<MarketAdmin> allAdmins(){
        MarketAdminExample adminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        return adminMapper.selectByExample(adminExample);
    }
}
